package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsOrderLog;
import com.efast.dmom.mes.bean.WmsPurchaseOrder;
import com.efast.dmom.mes.bean.WmsPurchaseOrderLog;
import com.efast.dmom.mes.dao.entity.WmsOrderLogDao;
import com.efast.dmom.mes.dao.entity.WmsPurchaseOrderDao;
import com.efast.dmom.mes.dao.entity.WmsPurchaseOrderLogDao;
import com.efast.dmom.mes.service.WmsPurchaseOrderService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderLogStatus;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;
@Service
@Transactional
public class WmsPurchaseOrderServiceImpl implements WmsPurchaseOrderService{

	@Autowired
	private WmsPurchaseOrderDao wmsPurchaseOrderDao;
	
	
	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;
	
	@Autowired
	private WmsPurchaseOrderLogDao wmsPurchaseOrderHistoryDao;
	
	@Autowired
	private WmsOrderLogDao wmsOrderLogDao;

	public WmsPurchaseOrderLog getOrderHistoryBean() {
		WmsPurchaseOrderLog wmsPurchaseOrderHistory = new WmsPurchaseOrderLog();
		wmsPurchaseOrderHistory.setCreateUser(SpringWebUtils.getRequestUser());
		wmsPurchaseOrderHistory.setCompanyCode(SpringWebUtils.getRequestCompany());
		wmsPurchaseOrderHistory.setCreateTime(TimeUtil.getLocalDateTime());
		return wmsPurchaseOrderHistory;
		
	}
	@Override
	public ResultBean save(List<WmsPurchaseOrder> wmsPurchaseOrder) {
		ResultBean resultBean = new ResultBean(true);
		String orderNo = serviceBaseEntityUtil.getNo("CGD_code");
		List <WmsOrderLog> logList = new ArrayList<WmsOrderLog>();
		wmsPurchaseOrder.forEach(item -> {
			item.setPurchaseOrderNo(orderNo);
			item.setStatus("待确认");
			ServiceBaseEntityUtil.setSaveBaseEntityData(item);
		});
		// 保存采购单信息
		wmsPurchaseOrderDao.batchSave(wmsPurchaseOrder);
		wmsPurchaseOrder.forEach(item -> {
			WmsOrderLog wmsOrderLog = new WmsOrderLog();
			wmsOrderLog.setOrderNo(orderNo);
			wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.ADD.getOperation());
			wmsOrderLog.setOrderId(item.getId());
			ServiceBaseEntityUtil.setSaveBaseEntityData(wmsOrderLog);
			logList.add(wmsOrderLog);
		});
		// 保存采购单日志信息
		wmsOrderLogDao.batchSave(logList);
		return resultBean;
	}

	@Override
	public ResultBean update(WmsPurchaseOrder wmsPurchaseOrder) {
		ResultBean resultBean = new ResultBean(true);
		int selectByPartId = wmsPurchaseOrderDao.selectByPartId(wmsPurchaseOrder);
		if (selectByPartId >0) {
			resultBean.setErrorMessage("已存在当前修改的物料编码，请检查！");
			return resultBean;
		}
		ServiceBaseEntityUtil.setUpdateBaseEntityData(wmsPurchaseOrder);
		WmsOrderLog wmsOrderLog = new WmsOrderLog();
		wmsOrderLog.setOrderId(wmsPurchaseOrder.getId());
		wmsOrderLog.setOrderNo(wmsPurchaseOrder.getPurchaseOrderNo());
		wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.UPDATE.getOperation());
		ServiceBaseEntityUtil.setSaveBaseEntityData(wmsOrderLog);
		// 保存采购单日志信息
		wmsOrderLogDao.save(wmsOrderLog);
		// 更新历史
		wmsPurchaseOrderDao.update(wmsPurchaseOrder);
		return resultBean;
	}

	@Override
	public ResultBean batchDelete(List<WmsPurchaseOrder> list) {
		ResultBean resultBean = new ResultBean(true);
		List<WmsOrderLog> historyList = new ArrayList<WmsOrderLog>();
		list.forEach(item -> {
			WmsOrderLog wmsOrderLog = new WmsOrderLog();
			wmsOrderLog.setOrderNo(item.getPurchaseOrderNo());
			wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.DELETE.getOperation());
			wmsOrderLog.setOrderId(item.getId());
			ServiceBaseEntityUtil.setSaveBaseEntityData(wmsOrderLog);
			historyList.add(wmsOrderLog);
		});
		// 批量新增删除时的日志
		wmsOrderLogDao.batchSave(historyList);
		// 批量删除
		wmsPurchaseOrderDao.batchDelete(list);
		return resultBean;
	}

	@Override
	public ResultBean batchConfirm(List<WmsPurchaseOrder> selectByOrderNo) {
		ResultBean resultBean = new ResultBean(true);
		List<WmsPurchaseOrder> list = wmsPurchaseOrderDao.selectByOrderNo(selectByOrderNo);
		List<WmsOrderLog> historyList = new ArrayList<WmsOrderLog>();
		list.forEach(item -> {
			item.setStatus("已确认");
			WmsOrderLog wmsOrderLog = new WmsOrderLog();
			wmsOrderLog.setOrderNo(item.getPurchaseOrderNo());
			wmsOrderLog.setOrderId(item.getId());
			wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.CONFIRM.getOperation());
			ServiceBaseEntityUtil.setSaveBaseEntityData(wmsOrderLog);
			historyList.add(wmsOrderLog);
		});
		// 批量新增确认时的日志
		wmsOrderLogDao.batchSave(historyList);
		// 批量新增确认数据
		wmsPurchaseOrderDao.batchConfirm(list);
		return resultBean;
	}
	@Override
	public ResultBean itemSave(WmsPurchaseOrder wmsPurchaseOrder) {
		ResultBean resultBean = new ResultBean(true);
		List<WmsPurchaseOrder> list = new ArrayList<WmsPurchaseOrder>();
		list.add(wmsPurchaseOrder);
		List<WmsPurchaseOrder> selectByOrderNo = wmsPurchaseOrderDao.selectByOrderNo(list);
		for (WmsPurchaseOrder wmsPurchaseOrder2 : selectByOrderNo) {
			if (wmsPurchaseOrder2.getPartId() == wmsPurchaseOrder.getPartId()) {
				 resultBean.setErrorMessage("当前新增物料已存在，请重新选择！");
				 return resultBean;
			}
		}
		ServiceBaseEntityUtil.setSaveBaseEntityData(wmsPurchaseOrder);
		wmsPurchaseOrder.setStatus(WmsOrderStatusUtil.NOCONFIRM);
		int save = wmsPurchaseOrderDao.save(wmsPurchaseOrder);
		if (save == 1) {
			WmsOrderLog wmsOrderLog = new WmsOrderLog();
			wmsOrderLog.setOrderNo(wmsPurchaseOrder.getPurchaseOrderNo());
			wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.ADD.getOperation());
			wmsOrderLog.setOrderId(wmsPurchaseOrder.getId());
			ServiceBaseEntityUtil.setUpdateLogEntityData(wmsOrderLog);
			wmsOrderLogDao.save(wmsOrderLog);
		}
		return resultBean;
	}
	@Override
	public ResultBean delete(List<WmsPurchaseOrder> dataByPurchaseNo) {
		ResultBean resultBean = new ResultBean(true);
		List<WmsPurchaseOrder> list = wmsPurchaseOrderDao.selectByOrderNo(dataByPurchaseNo);
		List<WmsOrderLog> historyList = new ArrayList<WmsOrderLog>();
		list.forEach(item -> {
			WmsOrderLog wmsOrderLog = new WmsOrderLog();
			wmsOrderLog.setOrderNo(item.getPurchaseOrderNo());
			wmsOrderLog.setOrderOperationStatus(WmsOrderLogStatus.DELETE.getOperation());
			wmsOrderLog.setOrderId(item.getId());
			ServiceBaseEntityUtil.setSaveBaseEntityData(wmsOrderLog);
			historyList.add(wmsOrderLog);
		});
		// 批量新增删除时的日志
		wmsOrderLogDao.batchSave(historyList);
		// 批量删除
		wmsPurchaseOrderDao.batchDelete(list);
		return resultBean;
	}




}
